{extend name="platform/new_base" /}
{block name="resources"/}
{/block}
{block name="main"}
                <form action="" class="form-horizontal form-validate pt-15 widthFixedForm">
                	<div class="form-group">
                		<label class="col-md-2 control-label"><span class="text-bright">*</span> 等级名称</label>
                		<div class="col-md-5">
                			<input type="text" class="form-control" id="level_name" name="level_name" required>
                		</div>
                	</div>
                	<div class="form-group">
                		<label class="col-md-2 control-label"><span class="text-bright">*</span> 升级条件</label>
                		<div class="col-md-5">
                               <div class="input-group w-400">
                                    <div class="input-group-addon fs-12">成长值达</div>
                                    <input type="number" id="growth_num" name="growth_num" class="form-control number-form-control " min="1" autocomplete="off" required>
                                </div>
                                <div class="help-block mb-0">数值越大，会员等级越高，只能输入整数</div>
                		</div>
                		
                	</div>
                	<div class="form-group">
                		<label class="col-md-2 control-label">优惠折扣</label>
                		<div class="col-md-5">
                			<input type="number" class="form-control" id="goods_discount" min="0.01" max="10">
                            <div class="help-block mb-0" >数值越大，优惠折扣越小, 推荐1-9.9之间的数字,10不打折</div>
                		</div>
                	</div>
					<div class="form-group">
						<label class="col-md-2 control-label">小数取整</label>
						<div class="col-md-5">
							<div class="switch-inline">
								<input type="checkbox" id="upgrade-switch" value="1" name="is_label">
								<label for="upgrade-switch"></label>
							</div>
							<div class="help-block mb-0" >折扣后价格小数四舍五入取整，例：198.55取整后199.00，198.46取整后198.00</div>
						</div>
					</div>
                	<div class="form-group"></div>
                	<div class="form-group">
                        <label class="col-md-2 control-label"></label>
                        <div class="col-md-8">
                            <button class="btn btn-primary addLevel" type="submit">添加</button>
                            <a href="javascript:history.go(-1);" class="btn btn-default">返回</a>
                        </div>
                    </div>
                </form>
<input type="hidden" id="level_growth_num" value="{$level_growth_num}">
{/block}
{block name="script"}
<script>
require(['util'],function(util){
    $("input[name='level_name']").on('blur',function () {
        var level_name = $("input[name='level_name']").val();
        $.ajax({
            type : "post",
            url : "{:__URL('PLATFORM_MAIN/Member/memberInfo')}",
            async : true,
            data : {
                'level_name' : level_name,
            },
            success : function(data) {
                if (data["code"] > 0) {
                    util.message('该等级已存在','danger');
                    $("input[name='level_name']").val('');
                    return false;
                }
            }
        });
    })
    $("input[name='growth_num']").on('blur',function () {
        var growth_num = $("input[name='growth_num']").val();
        var arr = $("#level_growth_num").val();
		arr=arr.split(',');
        for(var i = 0; i < arr.length; i++){
            if(growth_num === arr[i] ){
                util.message('该等级成长值已存在','danger');
                $("input[name='growth_num']").val('');
                return false;
            }
        }
    })
    util.validate($('.form-validate'),function(form){
        var goods_discount = $("#goods_discount").val();
        var level_name = $("#level_name").val();
        var growth_num = $("#growth_num").val();
        var is_label = $("input[name='is_label']:checked").val();
        if(goods_discount>10){
            util.message('折扣额度不能超过10折','danger');
			return false;
		}
		if(goods_discount<0.01){
            util.message('折扣额度不能小于0.01折','danger');
            return false;
		}
        $('.addLevel').attr({disabled: "disabled"}).html('提交中...');
        if(growth_num){
            $.ajax({
                type : "post",
                url : "{:__URL('PLATFORM_MAIN/Member/addMemberLevel')}",
                async : true,
                data : {
                    'level_name' : level_name,
                    'growth_num' : growth_num,
                    'goods_discount' : goods_discount,
					'is_label':is_label
                },
                success : function(data) {
                    if (data["code"] > 0) {
                        util.message(data["message"],'success',"{:__URL('PLATFORM_MAIN/member/memberLevelList')}");
                    }else if (data["code"] == -2) {
                        util.message('该等级已存在','danger');
                        $('.addLevel').removeAttr('disabled').html('添加');
                    }else{
                        util.message(data["message"],'danger');
                        $('.addLevel').removeAttr('disabled').html('添加');
                    }
                }
            });
		}else{
            util.message('请填写升级条件');
            return false;
		}

    })
})
</script>
{/block}